#! /bin/bash
export LANG=en_US.UTF-8

rsecs=0
rsecsmin=
rsecsmax=   
wsecs=0
wsecsmin=
wsecsmax=
await=0
awaitmin=
awaitmax=
util=0
utilmin=
utilmax=
quota=
QUOTASTR=

FLAGE="sda"

StatStr=
CURNUM=				#当前的性能参数

WranResultStr=		#最后形成的告警字符串
ErrorResultStr=		#最后形成的错误字符串
DataResultStr=		#性能呢个参数字符串

function gotErr(){
	if [ "$1" -eq 0 ];then
		echo "mailstatedes=各项指标恢复正常 statedes=各项指标恢复正常|$DataResultStr"
	elif [ "$1" -eq 1 ];then 
		echo "quota=${quota} mailstatedes=$WranResultStr statedes=$WranResultStr ${QUOTASTR} |$DataResultStr"
	elif [ "$1" -eq 2 ];then 
		if [[ "${WranResultStr}" != "" ]]; then
			echo "quota=${quota} mailstatedes=${WranResultStr},${ErrorResultStr} statedes=${WranResultStr},${ErrorResultStr} ${QUOTASTR}|$DataResultStr"
			else
			echo "quota=${quota} mailstatedes=${ErrorResultStr} statedes=${ErrorResultStr} ${QUOTASTR}|$DataResultStr"
		fi
		
	elif [ "$1" -eq 3 ];then 
		echo "mailstatedes=无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置 statedes=无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置"
	fi

	exit $1
}


function contain(){
	result=$(echo $1 | grep "$2")
	if [[ "$result" != "" ]]
	then
		return 1
	else
		return 0
	fi
}

#返回是否服务是否存活
function getInfo(){

	StatStr=$(iostat  -x 1 1 |grep "$FLAGE")
	if [ "$StatStr" == "" ];then
		gotErr 3
	fi

	contain "$StatStr" "iostat"
	if [ $? -eq 1 ];then
		gotErr 3
	fi

}

#依据传入的参数值来判断我们要监控的服务是什么
#然后通过各个函数获取到具体的值
#值之间的
function analysisStat(){

	if [ "$rsecs" -eq 1 ];then
		CURNUM=$(echo $StatStr | awk -F ' ' '{print $6} ')
		parseFileds "rsecs" $rsecsmin $rsecsmax
	fi
	if [ "$wsecs" -eq 1 ];then
		CURNUM=$(echo $StatStr | awk -F ' ' '{print $7} ')
		parseFileds "wsecs" $wsecsmin $wsecsmax
	fi
	if [ "$await" -eq 1 ];then
		CURNUM=$(echo $StatStr | awk -F ' ' '{print $10} ')
		parseFileds "await" $awaitmin $awaitmax
	fi
	if [ "$util" -eq 1 ];then
		CURNUM=$(echo $StatStr | awk -F ' ' '{print $NF} ')
		parseFileds "util" $utilmin $utilmax
	fi
}

#CURNUM			当前值
#WARNLIMIT 		告警的上限
#ERRORLIMIT 	异常的上限
#判断以上三个值并拼接结果字符串
function parseFileds(){
	val1=$(echo "$CURNUM $2"| awk '{if($1<=$2){print 1}else{print 0}}')
	val2=$(echo "$CURNUM $3"| awk '{if($1<=$2){print 1}else{print 0}}')

	DataResultStr="$DataResultStr $1=$CURNUM;$2;$3;0;0"
	if [ $val1 -eq 1 ];then
		return 0
	elif [[ $val1 -eq 0 && $val2 -eq 1 ]];then
		if [[ ${WranResultStr} != "" ]]; then
			WranResultStr="${WranResultStr},"
		fi

		WranResultStr="${WranResultStr}$1当前值为${CURNUM}超过告警值${2}"

		if [[ ${quota} != "" ]]; then
			quota="${quota},"
		fi

		quota="${quota}$1"

		QUOTASTR="${QUOTASTR}$1=$1当前值为${CURNUM}超过告警值${2} "

		return 1
	else
		if [[ ${ErrorResultStr} != "" ]]; then
			ErrorResultStr="${ErrorResultStr},"
		fi
		ErrorResultStr="${ErrorResultStr}$1当前值为${CURNUM}超过紧急值${3}"

		if [[ ${quota} != "" ]]; then
			quota="${quota},"
		fi

		quota="${quota}$1"

		QUOTASTR="${QUOTASTR}$1=$1当前值为${CURNUM}超过紧急值${3} "
		
		return 2
	fi
}

#分析结果 给出给出状态
function analysisResult(){
	if [[ "$WranResultStr" == "" && "$ErrorResultStr" == "" ]];then
		gotErr 0
	elif [[ "$ErrorResultStr" == "" ]];then
		gotErr 1
	else
		gotErr 2
	fi
}

#输出错误信息并且退出程序
function showHelp(){
	if [ "$1" != "" ];then
		echo "请输入$1"
	fi
	echo "check_iostat.sh 可以监听本地的io的状态 参数如下"
	echo "check_iostat.sh [-w rsec/s,wsec/s,await,%util] [-c rsec/s,wsec/s,await,%util] [-f <str>]"
	echo "-w 表示监控警告的值"
	echo "-c 表示异常值"
	echo "-f <str> 特征字符串 表示监控哪个磁盘"
	exit 1
}

#检查参数是不是数字
function isNumber(){
	isNum=$(echo "$1" | awk '{print($0~/^([0-9])+[\.]?([0-9])?$/)?1:0}')
	return $isNum
}

function isLarge(){

	isNumber $1
	if [[ $? -eq 0 ]]; then
		return 1
	fi

	isNumber $2
	if [[ $? -eq 0 ]]; then
		return 1
	fi

	isLarge=$(echo "$1 $2"| awk '{if($1>=$2){print 1}else{print 0}}')
	if [ $isLarge -eq 1 ];then
		return 0
	else
		return 1
	fi
}

function checkARG(){
	if [[ "$2" != "" && "$3" != "" ]];then
		isLarge "$3" "$2"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			$1=1
		fi
	fi
	return 0
}

#检查参数是否合法
#即参数是否是小数和整数
#对于守护进程的选择 这个值可以不验证
function checkALL(){


	if [[ "$rsecsmin" != "" || "$rsecsmax" != "" ]];then
		if [[ "$rsecsmax" == "" || "$rsecsmin" == "" ]]; then
			return 1
		fi

		isLarge "$rsecsmax" "$rsecsmin"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			rsecs=1
		fi
	fi

	if [[ "$wsecsmin" != "" || "$wsecsmax" != "" ]];then
		if [[ "$wsecsmax" == "" || "$wsecsmin" == "" ]]; then
			return 1
		fi

		isLarge "$wsecsmax" "$wsecsmin"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			wsecs=1
		fi
	fi

	if [[ "$awaitmin" != "" || "$awaitmax" != "" ]];then
		if [[ "$awaitmax" == "" || "$awaitmin" == "" ]]; then
			return 1
		fi

		isLarge "$awaitmax" "$awaitmin"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			await=1
		fi
	fi

	if [[ "$utilmin" != "" || "$utilmax" != "" ]];then
		if [[ "$utilmax" == "" || "$utilmin" == "" ]]; then
			return 1
		fi

		isLarge "$utilmax" "$utilmin"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			util=1
		fi
	fi

	return 0
}



function parseARG(){
	#it mean warning
	if [ $1 -eq 1 ];then
 		rsecsmin=$(echo $2 | awk -F ',' '{print $1}' | sed "s/[0-9]\.[0-9]//g")
 		wsecsmin=$(echo $2 | awk -F ',' '{print $2}' | sed "s/[0-9]\.[0-9]//g")
 		awaitmin=$(echo $2 | awk -F ',' '{print $3}' | sed "s/[0-9]\.[0-9]//g")
 		utilmin=$(echo $2 | awk -F ',' '{print $4}' | sed "s/[0-9]\.[0-9]//g")
 	else
 		rsecsmax=$(echo $2 | awk -F ',' '{print $1}' | sed "s/[0-9]\.[0-9]//g")
 		wsecsmax=$(echo $2 | awk -F ',' '{print $2}' | sed "s/[0-9]\.[0-9]//g")
 		awaitmax=$(echo $2 | awk -F ',' '{print $3}' | sed "s/[0-9]\.[0-9]//g")
 		utilmax=$(echo $2 | awk -F ',' '{print $4}' | sed "s/[0-9]\.[0-9]//g")
 	fi

}

while getopts "w:c:f:h" arg
do
	case $arg in
		w)
			parseARG 1 "$OPTARG"
			;;
		c)
			parseARG 2 "$OPTARG"
			;;
		f)
			FLAGE=$OPTARG
			;;
		h)
			showHelp
			;;
		?) 
			showHelp
			;;
	esac
done

if [[ "$FLAGE" == "" ]];then
	showHelp "特征项"
fi

checkALL
if [[ $? == 1 ]];then
	showHelp "正确参数"
fi


#插件正题部分 
#确定存活的
#启动
#分析
getInfo
analysisStat
analysisResult
